const __extends = require('./psd/util').__extends
const Module = require('coffeescript-module').Module

const File = require('./psd/file')

const LazyExecute = require('./psd/lazy_execute')

const Header = require('./psd/header')

const Resources = require('./psd/resources')

const LayerMask = require('./psd/layer_mask')

const Image = require('./psd/image')

const ShimsInit = require('./shims/init')

const PSD = (function (_super) {
  __extends(PSD, _super)
  PSD.extends(ShimsInit)
  PSD.extends(require('./psd/init'))

  PSD.Node = {
    Root: require('./psd/nodes/root')
  }

  function PSD (data) {
    this.file = new File(data)
    this.parsed = false
    this.header = null
    Object.defineProperty(this, 'layers', {
      get: function () {
        return this.layerMask.layers
      }
    })
  }

  PSD.prototype.parse = function () {
    if (this.parsed) {
      return
    }
    this.parseHeader()
    this.parseResources()
    this.parseLayerMask()
    this.parseImage()
    return this.parsed = true
  }

  PSD.prototype.parseHeader = function () {
    this.header = new Header(this.file)
    return this.header.parse()
  }

  PSD.prototype.parseResources = function () {
    let resources
    resources = new Resources(this.file)
    return this.resources = new LazyExecute(resources, this.file).now('skip').later('parse').get()
  }

  PSD.prototype.parseLayerMask = function () {
    let layerMask
    layerMask = new LayerMask(this.file, this.header)
    return this.layerMask = new LazyExecute(layerMask, this.file).now('skip').later('parse').get()
  }

  PSD.prototype.parseImage = function () {
    let image
    image = new Image(this.file, this.header)
    return this.image = new LazyExecute(image, this.file).later('parse').ignore('width', 'height').get()
  }

  PSD.prototype.tree = function () {
    return new PSD.Node.Root(this)
  }

  return PSD
})(Module)

module.exports = PSD
